package arrays;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author LiangMu
 * @version \$Id: P15.java, v 0.1, 2022年12月15日 12:34 LiangMu Exp $$
 */
public class P15 {
    public static void main(String[] args) {
        int[][] ints = new int[][]{{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}};
        setZeroes(ints);
        for (int[] it : ints) {
            System.out.println(Arrays.toString(it));
        }
    }

    public static void setZeroes(int[][] matrix) {
        int x = matrix.length;
        int y = matrix[0].length;
        Set<Integer> xSet = new HashSet<>();
        Set<Integer> ySet = new HashSet<>();
        for (int i = 0; i < x; ++i) {
            for (int j = 0; j < y; ++j) {
                if (matrix[i][j] == 0) {
                    xSet.add(i);
                    ySet.add(j);
                }
            }
        }
        xSet.forEach(it -> {
            for (int i = 0; i < y; ++i) {
                matrix[it][i] = 0;
            }
        });
        ySet.forEach(it -> {
            for (int i = 0; i < x; ++i) {
                matrix[i][it] = 0;
            }
        });
    }
}
